/**
 * created by dqn 2018/3/7
 * 关于登录、注册等验证相关的api的controller
 */

package main.controller;

import main.entity.WebUser;
import main.service.AuthorizationService;
import main.service.WebUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class AuthorizationController {
    @Autowired
    AuthorizationService authorizationService;
    @Autowired
    WebUserService webUserService;

    /**
     * 登录controller
     *
     * @param loginUser 表单
     * @return json: {result:'success/failed'}
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, String> login(WebUser loginUser, HttpSession httpSession) {
        Map<String, String> resultMap = new HashMap<>();
        String key = "result";
        String success = "success";
        String failed = "failed";
        resultMap.put("user", loginUser.toString());
        WebUser webUser = authorizationService.login(loginUser);
        if (webUser == null) {
            resultMap.put(key, failed);
            return resultMap;
        }
        httpSession.setAttribute("user", webUser);
        resultMap.put(key, success);
        return resultMap;
    }

    /**
     * 注册Controller
     *
     * @param webUser     表单
     * @param code        验证码
     * @param httpSession 当前session
     * @return json: {result:'success/failed/code error'}
     */
    @RequestMapping(value = "/sign", method = RequestMethod.POST)
    public Map<String, String> signUp(WebUser webUser, String code, HttpSession httpSession) {
        String sessionCode = (String) httpSession.getAttribute("checkCode");
        Map<String, String> resultMap = new HashMap<>();
        String key = "result";
        String success = "success";
        String failed = "failed";
        String codeError = "code error";
        String userDuplicated = "user duplicated";
        // TODO: 2018/3/7 接入短信
//        if ((sessionCode==null) || (!sessionCode.equals(code))){
//            resultMap.put(key,codeError);
//            return resultMap;
//        }

        if (webUser.getNumber().length()<=1){
            resultMap.put(key,"number not valid");
            return resultMap;
        }

        // 重复注册
        if (webUserService.isExistByNumber(webUser.getNumber())) {
            resultMap.put(key, userDuplicated);
            return resultMap;
        }
        WebUser webUser1 = authorizationService.signUp(webUser);
        if (webUser1 == null) {
            resultMap.put(key, failed);
            return resultMap;
        }
        resultMap.put(key, success);
        return resultMap;
    }

    /**
     * 注销
     *
     * @param httpSession
     * @return json {"result":"done"}
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Map<String, String> logout(HttpSession httpSession) {
        Map<String, String> result = new HashMap<>();
        WebUser webUser = (WebUser) httpSession.getAttribute("user");
        if (webUser != null) {
            httpSession.removeAttribute("user");
        }
        result.put("result", "done");
        return result;
    }
}
